﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using System.IO.IsolatedStorage;

namespace FindMe
{
    public partial class chat : PhoneApplicationPage
    {
        FindMeFriendServiceReference.FindMeFriendServiceClient serviceClient;
        public chat()
        {
            InitializeComponent();
            serviceClient = new FindMeFriendServiceReference.FindMeFriendServiceClient();
            serviceClient.SendMessageToFriendCompleted += new EventHandler<FindMeFriendServiceReference.SendMessageToFriendCompletedEventArgs>(serviceClient_SendMessageToFriendCompleted);

            if (App.pushChannel != null)
            {
                App.pushChannel.ShellToastNotificationReceived += new EventHandler<Microsoft.Phone.Notification.NotificationEventArgs>(pushChannel_ShellToastNotificationReceived);
            }
        }

        void pushChannel_ShellToastNotificationReceived(object sender, Microsoft.Phone.Notification.NotificationEventArgs e)
        {
            MessageBox.Show("Message Received!");
        }

        void serviceClient_SendMessageToFriendCompleted(object sender, FindMeFriendServiceReference.SendMessageToFriendCompletedEventArgs e)
        {
            MessageBox.Show("Status: " + e.Result);
        }

        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
                string username = settings["Username"].ToString();
                Guid sessionToken = (Guid)settings["SessionToken"];
                serviceClient.SendMessageToFriendAsync(username, sessionToken, txtToUsername.Text, txtMessage.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}